package org.smr.ministore.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.blade.msg.ErrorCode;
import org.blade.utils.IdGenUtils;
import org.blade.utils.Pager;
import org.blade.service.base.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smr.ministore.dao.BrandMapper;
import org.smr.ministore.dao.BrandOrganMapper;
import org.smr.ministore.entities.Brand;
import org.smr.ministore.entities.BrandOrgan;
import org.smr.ministore.entities.vo.Assign2OrganVO;
import org.smr.ministore.entities.vo.Assign2OrgansVO;
import org.smr.ministore.service.BrandService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("brandServiceImpl")
@Transactional
public class BrandServiceImpl extends AbstractService<Long, Brand> implements BrandService {

    private static Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);

    @Autowired
    private BrandMapper mapper;

    @Autowired
    private BrandOrganMapper brandOrganMapper;

    @Autowired
    public BrandServiceImpl(BrandMapper mapper) {
        super(mapper);
    }

    public int save(Brand entity) {
        super.saveAttatch(entity);
        mapper.insert(entity);
        return this.insertBrandOrgan(entity);
    }

    private BrandOrgan createBrandOrgan(Brand brand) {
        BrandOrgan brandOrgan = brand.createReference();
        IdGenUtils.saveAttatch(brandOrgan);
        return brandOrgan;
    }

    private int insertBrandOrgan(Brand entity) {
        BrandOrgan brandOrgan = createBrandOrgan(entity);
        brandOrgan.setIsDeleted(false);
        IdGenUtils.saveAttatch(brandOrgan);
        return this.brandOrganMapper.insert(brandOrgan);
    }

    private int updateBrandOrgan(Brand entity) {
        BrandOrgan brandOrgan = createBrandOrgan(entity);
        IdGenUtils.modifyAttach(brandOrgan);
        return this.brandOrganMapper.insert(brandOrgan);
    }

    public int updateById(Brand id) {
        super.modifyAttach(id);
        mapper.updateById(id);
        return updateBrandOrgan(id);
    }

    @Transactional(readOnly = true)
    public Brand queryById(Long id) {
        return mapper.selectById(id);
    }

    public int deleteById(Long id) {
        return mapper.deleteById(id);
    }

    @Transactional(readOnly = true)
    public Pager<Brand> paginate(Brand entity, Long pageSize, Long pageNum) {

        Pager<Brand> pager = new Pager(pageSize, pageNum);
        List list = mapper.pagination(entity, pager);
        pager.setEntities(list);
        return pager;
    }

    public int batchSave(List<Brand> entities) {
        this.batchSaveAttatch(entities);
        return mapper.batchInsert(entities);
    }


    public int batchModify(List<Brand> entities) {
        this.batchModifyAttatch(entities);
        return mapper.batchUpdate(entities);
    }


    public int deleteByIdLogical(Long id) {
        return mapper.deleteByIdLogical(id);
    }


    public int assign(Assign2OrganVO assign2Organ) {
        Brand brand = new Brand();
        List<BrandOrgan> references = brand.assignTo(assign2Organ);
        IdGenUtils.batchSaveAttach(references);
        return this.brandOrganMapper.batchInsert(references);
    }

    @Transactional(readOnly = true)
    public Brand queryByPrimaryKey(Brand brand) {
        return mapper.selectByPrimaryKey(brand);
    }


    public int assignToOrgans(Assign2OrgansVO assignee) {
        Brand brand = new Brand();
        brand.setCreateBy(assignee.getCreateBy());
        brand.setUpdateBy(assignee.getUpdateBy());

        List<BrandOrgan> brandOrganPersistence = this.brandOrganMapper.selectBy(assignee);

        brand.want2Assign(brandOrganPersistence, assignee);
        if (CollectionUtils.isEmpty(assignee.getAssignees())) {
            return ErrorCode.Persistence.NONE_EFFECT;
        }
        List<BrandOrgan> goodsOrgans = brand.assignTo(assignee);
        IdGenUtils.batchSaveAttach(goodsOrgans);
        return this.brandOrganMapper.batchInsert(goodsOrgans);
    }

    public Long getId() {
        return IdGenUtils.getNextLongId();
    }
}
